
from simhash import Simhash
from datasketch import MinHash
import mmh3

def compute_simhash(text):
    """计算文本的 SimHash 值（64 位）"""
    return Simhash(text, f=64).value

def compute_minhash(text, num_perm=128):
    """计算文本的 MinHash 对象（基于单词）"""
    minhash = MinHash(num_perm=num_perm)
    for word in text.split():
        hash_bytes = mmh3.hash(word).to_bytes(4, byteorder='big', signed=True)
        minhash.update(hash_bytes)
    return minhash

def hamming_distance(hash1, hash2):
    """计算两个整数形式的 SimHash 值之间的汉明距离"""
    x = hash1 ^ hash2
    distance = 0
    while x:
        distance += 1
        x &= x - 1
    return distance